package com.my.util.execl;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.entity.Curriculum;
import com.my.entity.GradeClass;
import com.my.entity.User;
import com.my.service.GradeClassService;
import com.my.service.UserService;
import com.my.util.execl.entity.StudentExcel;

public class StudentExcelListener extends AnalysisEventListener<StudentExcel> {

    private UserService service;

    private GradeClassService gcService;

    public StudentExcelListener(UserService service, GradeClassService gcService) {
        this.service = service;
        this.gcService = gcService;
    }

    @Override
    public void invoke(StudentExcel excel, AnalysisContext analysisContext) {
        User user = new User();
        user.setId(excel.getId());
        user.setName(excel.getName());
        user.setPhone(excel.getPhone());
        user.setAddress(excel.getAddress());
        user.setSex("女".equals(excel.getSex()) ? 0 : 1);
        user.setType(3);
        user.setMajor(excel.getMajor());
        GradeClass grade = gcService.getOne(new QueryWrapper<GradeClass>().eq("name", excel.getGradeName()));
        GradeClass gradeClass = gcService.getOne(new QueryWrapper<GradeClass>().eq("name", excel.getClassName()).eq("parent_id", grade.getId()));
        user.setGradeClassId(gradeClass.getId());
        service.save(user);
        user.setPassword(SecureUtil.md5(user.getId() + user.getPassword()));
        service.updateById(user);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) { }
}
